<?php
/**
 * 巡展基金主线流程
 * @author Administrator
 *
 */
class xunProcessModel extends MY_Model{
    function xunProcessModel() {
        parent::MY_Model();
        $this->table = 'XunProcess';
    }
    /**
     * 包装数据
     * @param array $list
     * @return Ambigous <string, unknown>
     */
    function packing($list = array()){
        //时间单位配置
        $this->CI->load->model('timeUnitModel');
        $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
        foreach ($list as $k => $v){
            //时间单位
            $list[$k]['timeUnitStr'] = isset($timeUnitList[$v['year']][$v['timeUnit']]) ? $timeUnitList[$v['year']][$v['timeUnit']] : '';
        }
        return $list;
    }
    /**
     * 包装后查询列表,并以时间单位为键
     * @param mix $where
     * @return Ambigous
     */
    function getNewListByTimeUnit($where, $limit = NULL, $offset = NULL, $colum = '', $orderby = ''){
        $findList = $this->getList($where, $limit, $offset, $colum, $orderby);
        $list = array();
        foreach ($findList as $k => $v){
            $list[(int)$v['timeUnit']] = $v;
        }
        return $this->packing($list);
    }
    /**
     * 获取主流程列表，并以专营店ID为主键
     * @param array $where
     * @param string $limit
     * @param string $offset
     * @param string $colum
     * @param string $orderby
     * @return array
     */
    function getNewListByStoreId($where, $limit = NULL, $offset = NULL, $colum = '', $orderby = ''){
        $findList = $this->getList($where, $limit, $offset, $colum, $orderby);
        $list = array();
        foreach ($findList as $k => $v){
            $list[(int)$v['storeId']] = $v;
        }
        return $list;
    }
    /**
     * 初始化主流程数据
     * @param array $data
     */
    function initData($data = array()){
        if (!isset($data['storeId']) || !$data['storeId']){
            showError('请设置店铺ID');
        }
        if (!isset($data['year']) || !$data['year']){
            showError('请设置年份');
        }
        if (!isset($data['timeUnit']) || !$data['timeUnit']){
            showError('请设置时间');
        }
        $procId = $this->upset($data, array('createTime'=>time()));
        if ($procId){
            return $procId;
        }else{
            showError('生成失败');
        }
    }
    /**
     * 
     * 获取主线流程ID
     * @param int $storeId
     * @param int $year
     * @param int $timeUnit
     * @return Ambigous <number, unknown>
     */
    function getProcId($storeId, $timeUnit, $year){
        $findData = $this->getData(array('storeId'=>$storeId, 'timeUnit'=>$timeUnit, 'year'=>$year), 'id');
        return isset($findData['id']) ? $findData['id'] : 0;
    }
    /**
     * 统计金额明细
     * @param int $procId 主线流程ID
     */
    function countMoneyDetail($procId){
        $initData = array(
            'fixedMoney'    => 0,//项目定额
            'budgetApply'   => 0,//预算申请
            'budgetAudit'   => 0,//预算审核
            'settleApply'   => 0,//结算申请
            'settleAudit'   => 0,//结算审核
            'settleMoney'   => 0,//结算金额
        );
        //初始化
        $data = array(
            'project'   => $initData,
            'total'     => $initData,
        );
        //如果procId为0，则直接返回空数据
        if (!$procId){
            return $data;
        }
        //项目金额
        $this->CI->load->model('xProjectModel');
        $projectMoney = $this->xProjectModel->getMoney($procId);
        $data['project'] = $projectMoney;
        //总计
        $data['total'] = array(
            'fixedMoney'  => $projectMoney['fixedMoney'],//项目定额
            'siteNum' => $projectMoney['siteNum'],//站点数
            'budgetApply' => $projectMoney['budgetApply'],//预算申请
            'budgetAudit' => $projectMoney['budgetAudit'],//预算审核
            'settleApply' => $projectMoney['settleApply'],//结算申请
            'settleAudit' => $projectMoney['settleAudit'],//结算审核
            'settleMoney' => $projectMoney['settleMoney'],//可结算金额
        );
        return $data;
    }
    /**
     * 更新金额
     * @param int $procId 主线流程ID
     */
    function updateMoney($procId){
        $findData = $this->getData($procId);
        $childMoney = $this->countMoneyDetail($procId);
        $data = array(
            'fixedMoney'  => $childMoney['total']['fixedMoney'],//项目定额
            'siteNum' => $childMoney['total']['siteNum'],//站点数
            'budgetApply' => $childMoney['total']['budgetApply'],//预算申请
            'budgetAudit' => $childMoney['total']['budgetAudit'],//预算审核
            'settleApply' => $childMoney['total']['settleApply'],//结算申请
            'settleAudit' => $childMoney['total']['settleAudit'],//结算审核
            'settleMoney' => $childMoney['total']['settleMoney'],//结算金额
        );
        //更新
        $this->edit($procId, $data);
    }
    /**
     * 统计各结算状态数
     * @param int $id 单店ID
     * @param number $isBubao
     * @return multitype:number unknown
     */
    function getSettleStatusCount($procId){
        $count = array(
            -1  => 0,//未提交
            0   => 0,//未审核
            1   => 0,//已通过
            2   => 0,//不通过
            3   => 0,//返回修改
            4   => 0,//部分通过
        );
        //单店基金-项目活动
        $this->CI->load->model('xProjectModel');
        $projectCount = $this->CI->xProjectModel->getSettleStatusCount($procId);
    
        $count[-1] = $projectCount[-1];
        $count[0] = $projectCount[0];
        $count[1] = $projectCount[1];
        $count[2] = $projectCount[2];
        $count[3] = $projectCount[3];
        $count[4] = $projectCount[4];
        return $count;
    }
    /**
     * 分析列表，统计出条件专营店ID集的总和
     * @param array $sellpointIds 专营店id集
     * @param array $list 以专营店id为键的列表
     * @return array
     */
    function countTotalListByStoreIds($storeIds = array(),$list = array()){
        $data = array(
            'storeCount' => 0,//申请店数
            'fixedMoney' => 0,//固定额度（双月额度）
            'siteNum' => 0,//站点数
            'budgetApply'=>0,//预算申请
            'settleApply'=>0,//结算申请
            'settleAudit'=>0,//结算审核
            'settleMoney' => 0,//结算金额
        );
        $filterStoreId = array_intersect($storeIds, array_keys($list));
        foreach ($filterStoreId as $storeId){
            if(isset($list[$storeId])){
                $data['storeCount'] ++;//申请店数累加
                $data['fixedMoney']     += $list[$storeId]['fixedMoney'];//固定额度(双月额度)
                $data['siteNum'] += $list[$storeId]['siteNum'];//站点数
                $data['budgetApply']    += $list[$storeId]['budgetApply'];//预算申请
                $data['budgetAudit']    += $list[$storeId]['budgetAudit'];//预算审核
                $data['settleApply']    += $list[$storeId]['settleApply'];//结算申请
                $data['settleAudit']    += $list[$storeId]['settleAudit'];//结算审核
                $data['settleMoney']    += $list[$storeId]['settleMoney'];//结算金额
            }
        }
        return $data;
    }
}